{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课后作业：实现Azure face人脸验证\n",
    "\n",
    "* 选用Azure Face - Find Similar API，实现人脸对比，具体实现如下：\n",
    "* Create facelist 创建并命名好你的facelist\n",
    "* Add face 添加至少10张face作为对比人脸数据\n",
    "* get查看facelists中的信息Detect 检测出你需要的人脸并获取faceid\n",
    "* Find similars 返回相似置信度\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码实操"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 建列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests #导入requests模块\n",
    "import json\n",
    "create_facelists_url = \"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/%7BfaceListId\" \n",
    "subscription_key = \"d4059f977ae540cb85963ef89c95599d\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1、create 创建一个装相片的列表\n",
    "data = {\n",
    "    \"name\": \"list_pm_c\",\n",
    "    \"userData\": \"我的同学们\",\n",
    "    \"recognitionModel\": \"recognition_02\"\n",
    "}\n",
    "params = {\n",
    "    # Request parameters    \n",
    "   \n",
    "    \"faceListId\":\"mimi1\"\n",
    "    \n",
    "#     'detectionModel': 'detection_01',\n",
    "}\n",
    "r_create = requests.put(create_facelists_url,headers=headers,params=params,json=data)\n",
    "# 请求成功，返回空字符串\n",
    "r_create.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_create.content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 测试一张脸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "#先加一张脸试试\n",
    "# 2、Add face\n",
    "add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/%7BfaceListId/persistedFaces\"\n",
    "# 特别注意观察文档，每一步的url都要不一样\n",
    "\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    # Request headers\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "img_url = \"http://liuyu19.gitee.io/api/img/1.jpg\"\n",
    "\n",
    "params_add_face={\n",
    "    \"faceListId\":\"mimi1\",\n",
    "    \"userData\":\"文淇\"\n",
    "}\n",
    "\n",
    "r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "# r = requests.post(add_face, headers=headers,params = params,json={\"url\": add_face_image})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaceId': '58dd0639-2103-4564-8acf-4582aafacca0'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 封装函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/%7BfaceListId/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "\n",
    "    params_add_face={\n",
    "        \"faceListId\":\"mimi1\",\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url,headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code#返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 顺便把查看列表的也给封装了\n",
    "# Get facelist\n",
    "def GetFacelist():\n",
    "    get_facelist_url = \"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/%7BfaceListId\"\n",
    "    data = {\n",
    "        \"faceListId\":\"mimi1\",\n",
    "    }\n",
    "    r_get_facelist = requests.get(get_facelist_url,headers=headers,params=data)\n",
    "    return r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 存脸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"http://liuyu19.gitee.io/api/img/1.jpg\",\"1\")\n",
    "AddFace(\"http://liuyu19.gitee.io/api/img/2.jpg\",\"2\")\n",
    "AddFace(\"http://liuyu19.gitee.io/api/img/3.jpg\",\"3\")\n",
    "AddFace(\"http://liuyu19.gitee.io/api/img/4.jpg\",\"4\")\n",
    "AddFace(\"http://liuyu19.gitee.io/api/img/5.jpg\",\"5\")\n",
    "AddFace(\"http://liuyu19.gitee.io/api/img/6.jpg\",\"6\")\n",
    "AddFace(\"http://liuyu19.gitee.io/api/img/7.jpg\",\"7\")\n",
    "AddFace(\"http://liuyu19.gitee.io/api/img/8.jpg\",\"8\")\n",
    "AddFace(\"http://liuyu19.gitee.io/api/img/9.jpg\",\"9\")\n",
    "AddFace(\"http://liuyu19.gitee.io/api/img/10.jpg\",\"10\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '9080f1a5-ea72-488e-a6cd-4b50bafa016d',\n",
       "   'userData': '丘小峰'},\n",
       "  {'persistedFaceId': '58dd0639-2103-4564-8acf-4582aafacca0',\n",
       "   'userData': '文淇'},\n",
       "  {'persistedFaceId': '3569d426-47c4-478c-8d9d-aaf75b690507', 'userData': '2'},\n",
       "  {'persistedFaceId': '6c7980c3-c91a-4a0d-b270-24621cbd21c4', 'userData': '2'},\n",
       "  {'persistedFaceId': 'b20a7f84-d822-42c6-b77d-5ed2838d5ce2', 'userData': '2'},\n",
       "  {'persistedFaceId': '0be4cd56-ab93-46be-9dd4-88696c2c9b57', 'userData': '2'},\n",
       "  {'persistedFaceId': 'b1cbe84b-92ac-400e-a924-dba6b8cb603c', 'userData': '2'},\n",
       "  {'persistedFaceId': '5a10e7e2-d2da-4962-bd38-5f2cc6b5b872', 'userData': '2'},\n",
       "  {'persistedFaceId': '00204fd1-9b8d-4e40-856d-3e0f0e7d0959', 'userData': '2'},\n",
       "  {'persistedFaceId': 'df500baa-1c0a-485c-ae1c-9513445a9dab', 'userData': '2'},\n",
       "  {'persistedFaceId': '04ef9993-3fe9-4cbc-bbc3-be7d1aa8c555', 'userData': '2'},\n",
       "  {'persistedFaceId': '86b50267-209e-4605-8ab7-d737a91bec00', 'userData': '2'},\n",
       "  {'persistedFaceId': 'a59ec0cc-797b-48f9-a857-5a3e23b50bd8', 'userData': '2'},\n",
       "  {'persistedFaceId': '38f7f608-8b22-4977-b939-255adac456ee', 'userData': '1'},\n",
       "  {'persistedFaceId': '6dfa409b-8b61-4a2d-b5d2-1553caf97669', 'userData': '2'},\n",
       "  {'persistedFaceId': 'c6e51ca2-c7cc-46ae-b408-c779420421ea', 'userData': '3'},\n",
       "  {'persistedFaceId': '4ffc3d87-91b3-474e-baa4-a80884ae5540', 'userData': '4'},\n",
       "  {'persistedFaceId': 'f8f9afc1-353e-49b8-ad86-163a521959ee', 'userData': '5'},\n",
       "  {'persistedFaceId': 'ece2c45d-c649-449f-bafb-18bdd5a991e6', 'userData': '6'},\n",
       "  {'persistedFaceId': 'e74da184-26e5-405d-82f7-8a235f3db821', 'userData': '7'},\n",
       "  {'persistedFaceId': '5f2e1618-e2dd-40a0-9aa6-83e670f60160', 'userData': '8'},\n",
       "  {'persistedFaceId': 'd3da80ff-a818-4334-b876-0053b754aede', 'userData': '9'},\n",
       "  {'persistedFaceId': 'afe3cca4-dafa-4229-9988-5969082579f3',\n",
       "   'userData': '10'},\n",
       "  {'persistedFaceId': '4f122d2c-7287-4b60-acaa-dc763245a1cd', 'userData': '1'},\n",
       "  {'persistedFaceId': '19569c28-4353-4b9e-a475-9e7b4903a535', 'userData': '2'},\n",
       "  {'persistedFaceId': '5ab7bc29-3bb4-422d-8509-28707b673589', 'userData': '3'},\n",
       "  {'persistedFaceId': 'fbc0306e-7737-4282-ba88-2f1ccb4543b1', 'userData': '4'},\n",
       "  {'persistedFaceId': '011180fa-9ffc-4c07-9123-9f76f542905c', 'userData': '5'},\n",
       "  {'persistedFaceId': '3acf492d-94e5-447c-b346-41e7422dd660', 'userData': '6'},\n",
       "  {'persistedFaceId': '8bd01ce6-7d7e-461e-9a37-9287496ec440', 'userData': '7'},\n",
       "  {'persistedFaceId': 'caf265fa-207c-4343-b3c3-1119e317fcf5', 'userData': '8'},\n",
       "  {'persistedFaceId': 'd0373e8b-da5c-408f-93b4-66bac0100d5f', 'userData': '9'},\n",
       "  {'persistedFaceId': 'd5f1937e-0890-48e3-88b8-643f80c73547',\n",
       "   'userData': '10'}],\n",
       " 'faceListId': 'mimi1',\n",
       " 'name': 'list_pm_c',\n",
       " 'userData': '我的同学们'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "GetFacelist()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 检测脸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '374c0ec6-ad1c-4789-8f6e-4ce2a3ce373c',\n",
       "  'faceRectangle': {'top': 167, 'left': 88, 'width': 206, 'height': 206},\n",
       "  'faceAttributes': {'smile': 1.0,\n",
       "   'headPose': {'pitch': -11.5, 'roll': 0.4, 'yaw': -2.6},\n",
       "   'gender': 'female',\n",
       "   'age': 19.0,\n",
       "   'facialHair': {'moustache': 0.0, 'beard': 0.0, 'sideburns': 0.0},\n",
       "   'glasses': 'NoGlasses',\n",
       "   'emotion': {'anger': 0.0,\n",
       "    'contempt': 0.0,\n",
       "    'disgust': 0.0,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 1.0,\n",
       "    'neutral': 0.0,\n",
       "    'sadness': 0.0,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.02},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.59},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.0},\n",
       "   'makeup': {'eyeMakeup': True, 'lipMakeup': True},\n",
       "   'accessories': [],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.22,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'black', 'confidence': 0.99},\n",
       "     {'color': 'gray', 'confidence': 0.65},\n",
       "     {'color': 'brown', 'confidence': 0.52},\n",
       "     {'color': 'other', 'confidence': 0.44},\n",
       "     {'color': 'blond', 'confidence': 0.19},\n",
       "     {'color': 'red', 'confidence': 0.01}]}}}]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3、检测人脸的id\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect'\n",
    "\n",
    "# 请求正文\n",
    "image_url = 'http://huangjieqi.gitee.io/picture_storage/liuyu19.png'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择model\n",
    "    'recognitionModel':'recognition_02',\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->字符串\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "我的脸 = response.json()[0][\"faceId\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 找相似"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "#4、Find similars\n",
    "\n",
    "findsimilars_url = \"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/findsimilars\"\n",
    "\n",
    "# 请求正文 faceId需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":我的脸,\n",
    "    \"faceListId\":\"mimi1\",\n",
    "    \"maxNumOfCandidatesReturned\": 10,\n",
    "    \"mode\": \"matchFace\"#matchPerson\n",
    "    }\n",
    "\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': 'f8f9afc1-353e-49b8-ad86-163a521959ee',\n",
       "  'confidence': 0.21575},\n",
       " {'persistedFaceId': '011180fa-9ffc-4c07-9123-9f76f542905c',\n",
       "  'confidence': 0.21575},\n",
       " {'persistedFaceId': 'c6e51ca2-c7cc-46ae-b408-c779420421ea',\n",
       "  'confidence': 0.12234},\n",
       " {'persistedFaceId': '5ab7bc29-3bb4-422d-8509-28707b673589',\n",
       "  'confidence': 0.12234},\n",
       " {'persistedFaceId': 'ece2c45d-c649-449f-bafb-18bdd5a991e6',\n",
       "  'confidence': 0.0957},\n",
       " {'persistedFaceId': '3acf492d-94e5-447c-b346-41e7422dd660',\n",
       "  'confidence': 0.0957},\n",
       " {'persistedFaceId': '5f2e1618-e2dd-40a0-9aa6-83e670f60160',\n",
       "  'confidence': 0.09517},\n",
       " {'persistedFaceId': 'caf265fa-207c-4343-b3c3-1119e317fcf5',\n",
       "  'confidence': 0.09517},\n",
       " {'persistedFaceId': 'd3da80ff-a818-4334-b876-0053b754aede',\n",
       "  'confidence': 0.09462},\n",
       " {'persistedFaceId': 'd0373e8b-da5c-408f-93b4-66bac0100d5f',\n",
       "  'confidence': 0.09462}]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 做表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '9080f1a5-ea72-488e-a6cd-4b50bafa016d',\n",
       "   'userData': '丘小峰'},\n",
       "  {'persistedFaceId': '58dd0639-2103-4564-8acf-4582aafacca0',\n",
       "   'userData': '文淇'},\n",
       "  {'persistedFaceId': '3569d426-47c4-478c-8d9d-aaf75b690507', 'userData': '2'},\n",
       "  {'persistedFaceId': '6c7980c3-c91a-4a0d-b270-24621cbd21c4', 'userData': '2'},\n",
       "  {'persistedFaceId': 'b20a7f84-d822-42c6-b77d-5ed2838d5ce2', 'userData': '2'},\n",
       "  {'persistedFaceId': '0be4cd56-ab93-46be-9dd4-88696c2c9b57', 'userData': '2'},\n",
       "  {'persistedFaceId': 'b1cbe84b-92ac-400e-a924-dba6b8cb603c', 'userData': '2'},\n",
       "  {'persistedFaceId': '5a10e7e2-d2da-4962-bd38-5f2cc6b5b872', 'userData': '2'},\n",
       "  {'persistedFaceId': '00204fd1-9b8d-4e40-856d-3e0f0e7d0959', 'userData': '2'},\n",
       "  {'persistedFaceId': 'df500baa-1c0a-485c-ae1c-9513445a9dab', 'userData': '2'},\n",
       "  {'persistedFaceId': '04ef9993-3fe9-4cbc-bbc3-be7d1aa8c555', 'userData': '2'},\n",
       "  {'persistedFaceId': '86b50267-209e-4605-8ab7-d737a91bec00', 'userData': '2'},\n",
       "  {'persistedFaceId': 'a59ec0cc-797b-48f9-a857-5a3e23b50bd8', 'userData': '2'},\n",
       "  {'persistedFaceId': '38f7f608-8b22-4977-b939-255adac456ee', 'userData': '1'},\n",
       "  {'persistedFaceId': '6dfa409b-8b61-4a2d-b5d2-1553caf97669', 'userData': '2'},\n",
       "  {'persistedFaceId': 'c6e51ca2-c7cc-46ae-b408-c779420421ea', 'userData': '3'},\n",
       "  {'persistedFaceId': '4ffc3d87-91b3-474e-baa4-a80884ae5540', 'userData': '4'},\n",
       "  {'persistedFaceId': 'f8f9afc1-353e-49b8-ad86-163a521959ee', 'userData': '5'},\n",
       "  {'persistedFaceId': 'ece2c45d-c649-449f-bafb-18bdd5a991e6', 'userData': '6'},\n",
       "  {'persistedFaceId': 'e74da184-26e5-405d-82f7-8a235f3db821', 'userData': '7'},\n",
       "  {'persistedFaceId': '5f2e1618-e2dd-40a0-9aa6-83e670f60160', 'userData': '8'},\n",
       "  {'persistedFaceId': 'd3da80ff-a818-4334-b876-0053b754aede', 'userData': '9'},\n",
       "  {'persistedFaceId': 'afe3cca4-dafa-4229-9988-5969082579f3',\n",
       "   'userData': '10'},\n",
       "  {'persistedFaceId': '4f122d2c-7287-4b60-acaa-dc763245a1cd', 'userData': '1'},\n",
       "  {'persistedFaceId': '19569c28-4353-4b9e-a475-9e7b4903a535', 'userData': '2'},\n",
       "  {'persistedFaceId': '5ab7bc29-3bb4-422d-8509-28707b673589', 'userData': '3'},\n",
       "  {'persistedFaceId': 'fbc0306e-7737-4282-ba88-2f1ccb4543b1', 'userData': '4'},\n",
       "  {'persistedFaceId': '011180fa-9ffc-4c07-9123-9f76f542905c', 'userData': '5'},\n",
       "  {'persistedFaceId': '3acf492d-94e5-447c-b346-41e7422dd660', 'userData': '6'},\n",
       "  {'persistedFaceId': '8bd01ce6-7d7e-461e-9a37-9287496ec440', 'userData': '7'},\n",
       "  {'persistedFaceId': 'caf265fa-207c-4343-b3c3-1119e317fcf5', 'userData': '8'},\n",
       "  {'persistedFaceId': 'd0373e8b-da5c-408f-93b4-66bac0100d5f', 'userData': '9'},\n",
       "  {'persistedFaceId': 'd5f1937e-0890-48e3-88b8-643f80c73547',\n",
       "   'userData': '10'}],\n",
       " 'faceListId': 'mimi1',\n",
       " 'name': 'list_pm_c',\n",
       " 'userData': '我的同学们'}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_facelist_url = \"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/facelists/{faceListId}\"\n",
    "\n",
    "data = {\n",
    "    \"faceListId\":\"mimi1\",\n",
    "\n",
    "}\n",
    "\n",
    "r_get_facelist = requests.get(get_facelist_url,headers=headers,params=data)\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>9080f1a5-ea72-488e-a6cd-4b50bafa016d</td>\n",
       "      <td>丘小峰</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>58dd0639-2103-4564-8acf-4582aafacca0</td>\n",
       "      <td>文淇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3569d426-47c4-478c-8d9d-aaf75b690507</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6c7980c3-c91a-4a0d-b270-24621cbd21c4</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b20a7f84-d822-42c6-b77d-5ed2838d5ce2</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0be4cd56-ab93-46be-9dd4-88696c2c9b57</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>b1cbe84b-92ac-400e-a924-dba6b8cb603c</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5a10e7e2-d2da-4962-bd38-5f2cc6b5b872</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>00204fd1-9b8d-4e40-856d-3e0f0e7d0959</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>df500baa-1c0a-485c-ae1c-9513445a9dab</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>04ef9993-3fe9-4cbc-bbc3-be7d1aa8c555</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>86b50267-209e-4605-8ab7-d737a91bec00</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>a59ec0cc-797b-48f9-a857-5a3e23b50bd8</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>38f7f608-8b22-4977-b939-255adac456ee</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>6dfa409b-8b61-4a2d-b5d2-1553caf97669</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>c6e51ca2-c7cc-46ae-b408-c779420421ea</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>4ffc3d87-91b3-474e-baa4-a80884ae5540</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>f8f9afc1-353e-49b8-ad86-163a521959ee</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>ece2c45d-c649-449f-bafb-18bdd5a991e6</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>e74da184-26e5-405d-82f7-8a235f3db821</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>5f2e1618-e2dd-40a0-9aa6-83e670f60160</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>d3da80ff-a818-4334-b876-0053b754aede</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>afe3cca4-dafa-4229-9988-5969082579f3</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>4f122d2c-7287-4b60-acaa-dc763245a1cd</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>19569c28-4353-4b9e-a475-9e7b4903a535</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>5ab7bc29-3bb4-422d-8509-28707b673589</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>fbc0306e-7737-4282-ba88-2f1ccb4543b1</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>011180fa-9ffc-4c07-9123-9f76f542905c</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>3acf492d-94e5-447c-b346-41e7422dd660</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>8bd01ce6-7d7e-461e-9a37-9287496ec440</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>caf265fa-207c-4343-b3c3-1119e317fcf5</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>d0373e8b-da5c-408f-93b4-66bac0100d5f</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>d5f1937e-0890-48e3-88b8-643f80c73547</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         persistedFaceId userData\n",
       "0   9080f1a5-ea72-488e-a6cd-4b50bafa016d      丘小峰\n",
       "1   58dd0639-2103-4564-8acf-4582aafacca0       文淇\n",
       "2   3569d426-47c4-478c-8d9d-aaf75b690507        2\n",
       "3   6c7980c3-c91a-4a0d-b270-24621cbd21c4        2\n",
       "4   b20a7f84-d822-42c6-b77d-5ed2838d5ce2        2\n",
       "5   0be4cd56-ab93-46be-9dd4-88696c2c9b57        2\n",
       "6   b1cbe84b-92ac-400e-a924-dba6b8cb603c        2\n",
       "7   5a10e7e2-d2da-4962-bd38-5f2cc6b5b872        2\n",
       "8   00204fd1-9b8d-4e40-856d-3e0f0e7d0959        2\n",
       "9   df500baa-1c0a-485c-ae1c-9513445a9dab        2\n",
       "10  04ef9993-3fe9-4cbc-bbc3-be7d1aa8c555        2\n",
       "11  86b50267-209e-4605-8ab7-d737a91bec00        2\n",
       "12  a59ec0cc-797b-48f9-a857-5a3e23b50bd8        2\n",
       "13  38f7f608-8b22-4977-b939-255adac456ee        1\n",
       "14  6dfa409b-8b61-4a2d-b5d2-1553caf97669        2\n",
       "15  c6e51ca2-c7cc-46ae-b408-c779420421ea        3\n",
       "16  4ffc3d87-91b3-474e-baa4-a80884ae5540        4\n",
       "17  f8f9afc1-353e-49b8-ad86-163a521959ee        5\n",
       "18  ece2c45d-c649-449f-bafb-18bdd5a991e6        6\n",
       "19  e74da184-26e5-405d-82f7-8a235f3db821        7\n",
       "20  5f2e1618-e2dd-40a0-9aa6-83e670f60160        8\n",
       "21  d3da80ff-a818-4334-b876-0053b754aede        9\n",
       "22  afe3cca4-dafa-4229-9988-5969082579f3       10\n",
       "23  4f122d2c-7287-4b60-acaa-dc763245a1cd        1\n",
       "24  19569c28-4353-4b9e-a475-9e7b4903a535        2\n",
       "25  5ab7bc29-3bb4-422d-8509-28707b673589        3\n",
       "26  fbc0306e-7737-4282-ba88-2f1ccb4543b1        4\n",
       "27  011180fa-9ffc-4c07-9123-9f76f542905c        5\n",
       "28  3acf492d-94e5-447c-b346-41e7422dd660        6\n",
       "29  8bd01ce6-7d7e-461e-9a37-9287496ec440        7\n",
       "30  caf265fa-207c-4343-b3c3-1119e317fcf5        8\n",
       "31  d0373e8b-da5c-408f-93b4-66bac0100d5f        9\n",
       "32  d5f1937e-0890-48e3-88b8-643f80c73547       10"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adf = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])# 升级pandas才能运行\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>f8f9afc1-353e-49b8-ad86-163a521959ee</td>\n",
       "      <td>0.21575</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>011180fa-9ffc-4c07-9123-9f76f542905c</td>\n",
       "      <td>0.21575</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>c6e51ca2-c7cc-46ae-b408-c779420421ea</td>\n",
       "      <td>0.12234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5ab7bc29-3bb4-422d-8509-28707b673589</td>\n",
       "      <td>0.12234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>ece2c45d-c649-449f-bafb-18bdd5a991e6</td>\n",
       "      <td>0.09570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3acf492d-94e5-447c-b346-41e7422dd660</td>\n",
       "      <td>0.09570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>5f2e1618-e2dd-40a0-9aa6-83e670f60160</td>\n",
       "      <td>0.09517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>caf265fa-207c-4343-b3c3-1119e317fcf5</td>\n",
       "      <td>0.09517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>d3da80ff-a818-4334-b876-0053b754aede</td>\n",
       "      <td>0.09462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>d0373e8b-da5c-408f-93b4-66bac0100d5f</td>\n",
       "      <td>0.09462</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  confidence\n",
       "0  f8f9afc1-353e-49b8-ad86-163a521959ee     0.21575\n",
       "1  011180fa-9ffc-4c07-9123-9f76f542905c     0.21575\n",
       "2  c6e51ca2-c7cc-46ae-b408-c779420421ea     0.12234\n",
       "3  5ab7bc29-3bb4-422d-8509-28707b673589     0.12234\n",
       "4  ece2c45d-c649-449f-bafb-18bdd5a991e6     0.09570\n",
       "5  3acf492d-94e5-447c-b346-41e7422dd660     0.09570\n",
       "6  5f2e1618-e2dd-40a0-9aa6-83e670f60160     0.09517\n",
       "7  caf265fa-207c-4343-b3c3-1119e317fcf5     0.09517\n",
       "8  d3da80ff-a818-4334-b876-0053b754aede     0.09462\n",
       "9  d0373e8b-da5c-408f-93b4-66bac0100d5f     0.09462"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bdf = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行\n",
    "bdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>f8f9afc1-353e-49b8-ad86-163a521959ee</td>\n",
       "      <td>5</td>\n",
       "      <td>0.21575</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>011180fa-9ffc-4c07-9123-9f76f542905c</td>\n",
       "      <td>5</td>\n",
       "      <td>0.21575</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>c6e51ca2-c7cc-46ae-b408-c779420421ea</td>\n",
       "      <td>3</td>\n",
       "      <td>0.12234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5ab7bc29-3bb4-422d-8509-28707b673589</td>\n",
       "      <td>3</td>\n",
       "      <td>0.12234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ece2c45d-c649-449f-bafb-18bdd5a991e6</td>\n",
       "      <td>6</td>\n",
       "      <td>0.09570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>3acf492d-94e5-447c-b346-41e7422dd660</td>\n",
       "      <td>6</td>\n",
       "      <td>0.09570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5f2e1618-e2dd-40a0-9aa6-83e670f60160</td>\n",
       "      <td>8</td>\n",
       "      <td>0.09517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>caf265fa-207c-4343-b3c3-1119e317fcf5</td>\n",
       "      <td>8</td>\n",
       "      <td>0.09517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d3da80ff-a818-4334-b876-0053b754aede</td>\n",
       "      <td>9</td>\n",
       "      <td>0.09462</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>d0373e8b-da5c-408f-93b4-66bac0100d5f</td>\n",
       "      <td>9</td>\n",
       "      <td>0.09462</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId userData  confidence\n",
       "1  f8f9afc1-353e-49b8-ad86-163a521959ee        5     0.21575\n",
       "6  011180fa-9ffc-4c07-9123-9f76f542905c        5     0.21575\n",
       "0  c6e51ca2-c7cc-46ae-b408-c779420421ea        3     0.12234\n",
       "5  5ab7bc29-3bb4-422d-8509-28707b673589        3     0.12234\n",
       "2  ece2c45d-c649-449f-bafb-18bdd5a991e6        6     0.09570\n",
       "7  3acf492d-94e5-447c-b346-41e7422dd660        6     0.09570\n",
       "3  5f2e1618-e2dd-40a0-9aa6-83e670f60160        8     0.09517\n",
       "8  caf265fa-207c-4343-b3c3-1119e317fcf5        8     0.09517\n",
       "4  d3da80ff-a818-4334-b876-0053b754aede        9     0.09462\n",
       "9  d0373e8b-da5c-408f-93b4-66bac0100d5f        9     0.09462"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(adf, bdf,how='inner', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
